<?php

class Application_Model_PerfilAcesso extends Zend_Db_Table_Abstract {

    protected $_name = 'perfil_acesso';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_PerfilAcesso();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert($data,$modules) {
        if (isset($data['descricao']) && isset($data['dt_cadastro'])){
            $codigo = parent::insert($data);
            
            if(isset($modules) && count($modules) > 0 ){
                Application_Model_ModuloPerfilAcesso::getInstance()->insere($modules,$codigo);
            }
        }
        
    }

    public function update(array $data, $modules) {
        $where = 'codigo = ' . $data['codigo'];
        parent::update($data, $where);
        
        if(isset($modules) && count($modules) > 0 ){
            Application_Model_ModuloPerfilAcesso::getInstance()->removeByPerfilAcesso($data['codigo']);
            Application_Model_ModuloPerfilAcesso::getInstance()->insere($modules,$data['codigo']);
        }
        
    }

    public function getById($id) {
        return $this->fetchRow("codigo = " . $id ." and dt_exclusao is null");
    }
    
    public function getByName($name) {
        return $this->fetchRow("descricao = '" . $name ."' and dt_exclusao is null");
    }

    public function getAll(){
        $sql = 'select descricao,codigo from perfil_acesso where dt_exclusao is null';
        return $this->getAdapter()->query($sql);
    }


    public function lista($page, $qntPagina, $orderName, $order, $descricao, $dtCadastro) {
        $sql = 'select codigo,descricao,dt_cadastro
                from perfil_acesso
                where dt_exclusao is null ';
       
        if(isset($descricao) && !empty($descricao)){
            $sql.=' and descricao like \''. $descricao. '%\'';
        }
        
        if(isset($dtCadastro) && !empty($dtCadastro)){
            $sql.=' and dt_cadastro = \''. Util_Function_Util::formataData($dtCadastro, 'Y/M/d'). '\'';
        }
        
        if (isset($orderName) && isset($order)) {
            $sql .= ' order by ' . $orderName . ' ' . $order;
        }
        
        $rs = $this->getAdapter()->query($sql)->fetchAll();
        $paginator = Zend_Paginator::factory($rs);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($qntPagina);

        return $paginator;
    }

    public function remove($id){
        $perfil = $this->getById($id)->toArray();
        $where = ' codigo = ' .$id;
        $perfil['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
        parent::update($perfil, $where);
        Application_Model_ModuloPerfilAcesso::getInstance()->removeByPerfilAcesso($id);
    }
    
    public function getDescricaoPerfis(){
        $sql = 'select descricao from perfil_acesso where dt_exclusao is null';
        $list = $this->getAdapter()->fetchAll($sql);
        
        $retorno;
        if(count($list)> 0){
            foreach ($list as $value) {
                $retorno[] = $value['descricao'];
            }
        }
        
        return $retorno;
    }
 
    public function getList(){
        return $this->fetchAll("dt_exclusao is null ");
    }
}